class Network {
  get SERVER_URL() {
    return "http://vpdami.mooddo.com";
    // return "https://mhdz.mooddo.com";
  }
  // 发起请求
  request(url, data, method = 'GET' ) {
    return new Promise((resolve, reject) => {
      const success = (res) => {
        if (res.data.IsSuccess) {
          return resolve(res.data.Data);
        }
        if ( 401 != res.data.Code ) {
          wx.showToast({
            icon: "none",
            title: res.data.Message,
          });
        }
        return reject(Object.assign(res, {url}));
      };
      const token = wx.getStorageSync('token');
      wx.request({
        url,
        data,
        header: {Authorization: `Bearer ${token}`, websitetype: 3},
        method,
        success,
        fail: (e) => {
          wx.showToast({
            icon: "none",
            title: res.data.Message,
          });
          reject(e);
        }
      })
    });
  }
  // get请求
  get(url, data) {
    return this.request(url, data, 'GET');
  }

  // post 请求
  post(url, data) {
    return this.request(url, data, 'POST');
  }
  
}

module.exports = new Network();